#!/usr/bin/python3

from optparse import OptionParser
import sys
import os
import re

sys.path.append("/usr/lib/linx-sec-center")
import ct_core

def create_parser():
    USAGE_MES="\n %prog sub     Get mac subject labels values\
               \n %prog def     Get mac default labels values\
               \n %prog rul     Get mac policy values\
               \n %prog mbc     Get mbc labels values\
               \n %prog doi     Get mac netlabel doi labels values\
               \n %prog amb     Get mac netlabel ambient labels values\
               \n %prog cat     Get mac netlabel categories labels values\
               \n %prog PID     Get process mac and mbc labels(PID is a process id)"
    parser = OptionParser(usage=USAGE_MES,version="%prog 1.0")
    return parser

def get_sub():
    ct_core.get_mac_sub_label()
    if not ct_core.d_sub_label:
        print("\nThe mac subject labels is empty!")
        return
    else:
        print("\nThe mac subject labels is:"),
    for i in ct_core.d_sub_label:
        print((" %5s: %s %s %s") %(i, ct_core.d_sub_label[i].field1,
            ct_core.d_sub_label[i].field2, ct_core.d_sub_label[i].field3))

def get_def():
    f1, f2, f3 = ct_core.get_mac_def_label()
    print("\nThe mac default labels is:\n"),
    print((" %s %s %s") %(f1, f2, f3))

def get_rul():
    ct_core.get_mac_rules_label()
    if not ct_core.d_rules_label:
        print("\nThe mac policy is empty!")
        return
    else:
        print("\nThe mac policy is:\n"),
    for i in ct_core.d_rules_label:
        print((" %s %s") %(i, ct_core.d_rules_label[i]))

def get_mbc():
    ct_core.get_mbc_label()
    if not ct_core.d_mbc_label:
        print("\nThe mbc policy is empty!")
        return
    else:
        print("\nThe mbc policy is:"),
    for s in ct_core.d_mbc_label:
        print("\n---------------------")
        print("%s" %s)
        for o in ct_core.d_mbc_label[s]:
            print(("%s") %o)
    print("---------------------")

def get_doi():
    doi = ct_core.get_mac_net_doi()
    print("\nDoi: %s" %doi)

def get_amb():
    ct_core.get_mac_net_amb()
    if not ct_core.d_amb_label:
        print("\nThe netlabel ambient is empty!")
        return
    else:
        print("\nThe netlabel ambient is:\n"),
        for i in ct_core.d_amb_label:
            print("%s: %s, %s, %s" %(i, ct_core.d_amb_label[i].net_field1,\
                    ct_core.d_amb_label[i].net_field2, ct_core.d_amb_label[i].net_field3))

def get_cat():
    f,s,t = ct_core.get_mac_net_cat()
    if s == '-':s=''
    if t == '-':t=''
    print("\nCategories: %s %s %s" %(f, s, t))

def get_PID(pid):
    f1, f2, f3 = ct_core.get_process_mac(pid)
    print("\nProcess [%s] mac label is: %s, %s, %s" %(pid, f1, f2, f3))
    print("\nProcess [%s] mbc label is: \n%s" %(pid, ct_core.get_process_mbc(pid)))


def main():
    parser= create_parser()
    args,labels = parser.parse_args()

    if not labels:
        parser.error("Argument error, please specify label.")

    l = labels[0]
    if re.match('^\d*$', l):
        get_PID(l)
    else:
        exec('get_%s()' %(l))


main()
